home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 04 - 1988 / 04.04 Apr 88 / TearOffPalette Source / Window.c < prev   
Encoding:
C/C++ Source or Header  |  1988-02-18  |  8.0 KB  |  312 lines  |  [TEXT/KAHL]

  1. /*
  2. ----------------------------------------------------------------------------------------------------
  3. T E A R O F F   P A L E T T E
  4.  
  5.     version 1.0
  6.     by Don Melton and Mike Ritter
  7.     
  8.     Copyright (C)1987, 1988 by Impulse Technologies, Inc., all rights reserved. 
  9.     
  10.     Filename:            Window.c
  11.     Font:                    Monaco, 9 point
  12.     Tab setting:    2
  13.     Compiler:            LightspeedC 2.15, Project type: APPL, Creator: TOPD
  14.     Segment:            Main */
  15.  
  16.  
  17.  
  18. /*
  19. ----------------------------------------------------------------------------------------------------
  20. GLOBAL CONSTANT DEFINITIONS AND VARIABLE DECLARATIONS */
  21.  
  22. #include "Constants.h"
  23. #include "Variables.h"
  24.  
  25. /*
  26. ----------------------------------------------------------------------------------------------------
  27. EXTERNAL FUNCTION DECLARATIONS */
  28.  
  29. extern short GoodNewPtr();                                    /* Error.c */
  30. extern short GoodResource();                                /* Error.c */
  31. extern short GetMenuBarHeight();                        /* Interface.c */
  32. extern pascal void DrawToolMenu();                    /* Menu.c */
  33. extern pascal void HiliteToolItem();                /* Menu.c */
  34. extern pascal void DrawPatternMenu();                /* Menu.c */
  35. extern pascal void HilitePatternItem();            /* Menu.c */
  36. extern pascal void DrawColorMenu();                    /* Menu.c */
  37. extern pascal void HiliteColorItem();                /* Menu.c */
  38.  
  39. /*
  40. ----------------------------------------------------------------------------------------------------
  41. FORWARD FUNCTION DECLARATIONS */
  42.  
  43. void NewDocument();
  44. Rect *GetDocumentRect();
  45. short NewScrollBars();
  46. Rect *GetHScrollBarRect();
  47. Rect *GetVScrollBarRect();
  48. void CloseDocument();
  49. void ActivateDocument();
  50. void UpdateDocument();
  51. void SizeDocument();
  52. void MoveSizeScrollBar();
  53. void UpdatePalette();
  54.  
  55.  
  56.  
  57. /*
  58. ----------------------------------------------------------------------------------------------------
  59. NEW DOCUMENT */
  60.  
  61. void NewDocument() {
  62.     StringHandle title;
  63.     WindowPtr theDocument;
  64.     Rect windowRect;
  65.     
  66.     if(!GoodResource(title = GetString(DOCUMENT_TITLE_STRING_ID))) {
  67.         return;
  68.     }
  69.     HLock(title);
  70.     
  71.     if(!GoodNewPtr(theDocument = NewWindow(nil,
  72.             GetDocumentRect(&windowRect),
  73.             *title,
  74.             NOT_VISIBLE,
  75.             zoomDocProc,
  76.             BottomPalette,
  77.             GO_AWAY_BOX,
  78.             DOCUMENT_WINDOW))) {
  79.         HUnlock(title);
  80.         
  81.         return;
  82.     }
  83.     if(!NewScrollBars(theDocument)) {
  84.         CloseDocument(theDocument);
  85.         PositionCounter += 1;
  86.         HUnlock(title);
  87.         
  88.         return;
  89.     }
  90.     PositionCounter += 1;
  91.     HUnlock(title);
  92.     
  93.     if(TopPalette != nil) {
  94.     
  95.         if(TopPalette != TopWindow) {
  96.             /* Bring the TopPalette to the front and generate activate event. */
  97.             SelectWindow(TopPalette);
  98.         }
  99.         else {
  100.             HiliteWindow(theDocument, true);
  101.         }
  102.         if(TopDocument != nil) {
  103.             /* Ensure the TopDocument and its controls are unhilited properly. */
  104.             ActivateDocument(TopDocument, false);
  105.         }
  106.     }
  107.     ShowWindow(theDocument);
  108. }
  109.  
  110. /*
  111. ----------------------------------------------------------------------------------------------------
  112. GET DOCUMENT RECT */
  113.  
  114. Rect *GetDocumentRect(windowRect)
  115. Rect *windowRect;
  116. {
  117.     short position;
  118.     
  119.     position = (PositionCounter + DOCUMENT_COUNT) % DOCUMENT_COUNT;
  120.     
  121.     windowRect->left = SCREEN_MARGIN
  122.             + (HORIZONTAL_WINDOW_OFFSET * position);
  123.     windowRect->top = GetMenuBarHeight() + TITLE_BAR_HEIGHT + SCREEN_MARGIN
  124.             + (VERTICAL_WINDOW_OFFSET * position);
  125.     windowRect->right = screenBits.bounds.right - SCREEN_MARGIN
  126.             - (((DOCUMENT_COUNT - 1) - position) * HORIZONTAL_WINDOW_OFFSET);
  127.     windowRect->bottom = screenBits.bounds.bottom - SCREEN_MARGIN
  128.             - (((DOCUMENT_COUNT - 1) - position) * VERTICAL_WINDOW_OFFSET);
  129.     
  130.     return(windowRect);
  131. }
  132.  
  133. /*
  134. ----------------------------------------------------------------------------------------------------
  135. NEW SCROLL BARS */
  136.  
  137. short NewScrollBars(whichWindow)
  138. WindowPtr whichWindow;
  139. {
  140.     Rect sBRect;
  141.     
  142.     if(GoodNewHandle(NewControl(whichWindow,
  143.             GetHScrollBarRect(whichWindow, &sBRect),
  144.             NULL,
  145.             VISIBLE,
  146.             0, 0, 255,
  147.             scrollBarProc,
  148.             NO_REFCON))) {
  149.         
  150.         if(GoodNewHandle(NewControl(whichWindow,
  151.             GetVScrollBarRect(whichWindow, &sBRect),
  152.                 NULL,
  153.                 VISIBLE,
  154.                 0, 0, 255,
  155.                 scrollBarProc,
  156.                 NO_REFCON))) {
  157.             return(true);
  158.         }
  159.     }
  160.     return(false);
  161. }
  162.  
  163. /*
  164. ----------------------------------------------------------------------------------------------------
  165. GET HORIZONTAL SCROLL BAR RECTANGLE */
  166.  
  167. Rect *GetHScrollBarRect(whichWindow, whichRect)
  168. WindowPtr whichWindow;
  169. Rect *whichRect;
  170. {
  171.     *whichRect = whichWindow->portRect;
  172.     
  173.     whichRect->left -= 1;
  174.     whichRect->top = whichRect->bottom - (SCROLL_BAR_SIZE - 1);
  175.     whichRect->right -= (SCROLL_BAR_SIZE - 2);
  176.     whichRect->bottom += 1;
  177.     
  178.     return(whichRect);
  179. }
  180.  
  181. /*
  182. ----------------------------------------------------------------------------------------------------
  183. GET VERTICAL SCROLL BAR RECTANGLE */
  184.  
  185. Rect *GetVScrollBarRect(whichWindow, whichRect)
  186. WindowPtr whichWindow;
  187. Rect *whichRect;
  188. {
  189.     *whichRect = whichWindow->portRect;
  190.     
  191.     whichRect->left = whichRect->right - (SCROLL_BAR_SIZE - 1);
  192.     whichRect->top -= 1;
  193.     whichRect->right += 1;
  194.     whichRect->bottom -= (SCROLL_BAR_SIZE - 2);
  195.     
  196.     return(whichRect);
  197. }
  198.  
  199. /*
  200. ----------------------------------------------------------------------------------------------------
  201. CLOSE DOCUMENT
  202.  
  203.     A REAL application would need a more complex CloseDocument() function. This is just a shell. */
  204.  
  205. void CloseDocument(whichWindow)
  206. WindowPtr whichWindow;
  207. {
  208.     DisposeWindow(whichWindow);
  209.     
  210.     PositionCounter -= 1;
  211. }
  212.  
  213. /*
  214. ----------------------------------------------------------------------------------------------------
  215. ACTIVATE DOCUMENT */
  216.  
  217. void ActivateDocument(whichWindow, activate)
  218. WindowPtr whichWindow;
  219. Boolean activate;
  220. {
  221.     HiliteWindow(whichWindow, activate);
  222.     
  223.     if(activate) {
  224.         ShowControl(((WindowPeek) whichWindow)->controlList);
  225.         ShowControl((*((WindowPeek) whichWindow)->controlList)->nextControl);
  226.     }
  227.     else {
  228.         SetPort(whichWindow);
  229.         HideControl(((WindowPeek) whichWindow)->controlList);
  230.         ValidRect(&(*((WindowPeek) whichWindow)->controlList)->contrlRect);
  231.         HideControl((*((WindowPeek) whichWindow)->controlList)->nextControl);
  232.         ValidRect(&(*(*((WindowPeek) whichWindow)->controlList)->nextControl)->contrlRect);
  233.     }
  234.     DrawGrowIcon(whichWindow);
  235. }
  236.  
  237. /*
  238. ----------------------------------------------------------------------------------------------------
  239. UPDATE DOCUMENT
  240.  
  241.     A REAL application would need a more complex UpdateDocument() function. This is just a shell. */
  242.  
  243. void UpdateDocument(whichDocument)
  244. WindowPtr whichDocument;
  245. {
  246.     EraseRect(&whichDocument->portRect);
  247. }
  248.  
  249. /*
  250. ----------------------------------------------------------------------------------------------------
  251. SIZE DOCUMENT */
  252.  
  253. void SizeDocument(whichDocument)
  254. WindowPtr whichDocument;
  255. {
  256.     Rect sBRect;
  257.     
  258.     MoveSizeScrollBar(((WindowPeek) whichDocument)->controlList,
  259.             GetHScrollBarRect(whichDocument, &sBRect));
  260.     MoveSizeScrollBar((*((WindowPeek) whichDocument)->controlList)->nextControl,
  261.             GetVScrollBarRect(whichDocument, &sBRect));
  262.     
  263.     BeginUpdate(whichDocument);
  264.     UpdateDocument(whichDocument);
  265.     EndUpdate(whichDocument);
  266.     
  267.     ShowControl(((WindowPeek) whichDocument)->controlList);
  268.     ShowControl((*((WindowPeek) whichDocument)->controlList)->nextControl);
  269.     
  270.     DrawGrowIcon(whichDocument);
  271. }
  272.  
  273. /*
  274. ----------------------------------------------------------------------------------------------------
  275. MOVE AND SIZE SCROLL BAR */
  276.  
  277. void MoveSizeScrollBar(whichScrollBar, sBRect)
  278. ControlHandle whichScrollBar;
  279. Rect *sBRect;
  280. {
  281.     HideControl(whichScrollBar);
  282.     
  283.     MoveControl(whichScrollBar, sBRect->left, sBRect->top);
  284.     SizeControl(whichScrollBar, sBRect->right - sBRect->left, sBRect->bottom - sBRect->top);
  285. }
  286.  
  287. /*
  288. ----------------------------------------------------------------------------------------------------
  289. UPDATE PALETTE */
  290.  
  291. void UpdatePalette(whichPalette)
  292. WindowPtr whichPalette;
  293. {
  294.     /* Use the palette's refCon to determine which palette needs updating. */
  295.     switch(((WindowPeek) whichPalette)->refCon) {
  296.     
  297.         case TOOL_PALETTE:
  298.             DrawToolMenu(&whichPalette->portRect);
  299.             HiliteToolItem(&whichPalette->portRect, (*TearOffs[TOOL_PALETTE])->currentItem, true);
  300.             break;
  301.             
  302.         case PATTERN_PALETTE:
  303.             DrawPatternMenu(&whichPalette->portRect);
  304.             HilitePatternItem(&whichPalette->portRect, (*TearOffs[PATTERN_PALETTE])->currentItem, true);
  305.             break;
  306.             
  307.         case COLOR_PALETTE:
  308.             DrawColorMenu(&whichPalette->portRect);
  309.             HiliteColorItem(&whichPalette->portRect, (*TearOffs[COLOR_PALETTE])->currentItem, true);
  310.     }
  311. }
  312.